﻿using System;

using Infrastructure.NetExtensions;
using TempEmployees.BLL.Database;
using TempEmployees.BLL.EntitiesExtensions;

namespace TempEmployees.Web
{
    public partial class WebForm5 : ABasePage
    {
        private static readonly string BUTTON_UPDATE_EMPLOYEE = WebConfigData.BUTTON_UPDATE_EMPLOYEE;
        private static readonly string BUTTON_ENABLE_UPDATE = WebConfigData.BUTTON_ENABLE_UPDATE;

        private static readonly string EMPLOYEE_ID = "EMPLOYEE_ID";
        private string EmployeeId
        {
            set
            {
                ViewState.Set<string>(EMPLOYEE_ID, value);
            }
            get
            {
                return ViewState.Get<string>(EMPLOYEE_ID);
            }
        }

        private static readonly string SOURCE_PAGE = "SOURCE_PAGE";
        private string SourcePage
        {
            set
            {
                ViewState.Set<string>(SOURCE_PAGE, value);
            }
            get
            {
                return ViewState.Get<string>(SOURCE_PAGE);
            }
        }

        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            //page defualt button
            Page.Form.DefaultButton = ButtonUpdateEmployee.UniqueID;
            //gridview delegate
            GridViewAssignments1.GotoAssignmentDetailes = GotoAssignmentDetailes;

            if (!IsPostBack)
            {
                GetPageData();
                SetAssignmentsGridView();
                SetButtons();
            }
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EmployeeDetails1.SetEmployeeID(EmployeeId);
                EmployeeDetails1.ReadonlyControls(true);
            }
        }

        private void SetAssignmentsGridView()
        {
            Filters filters = new Filters();
            string employeeId = EEmployee.GetEmployeeByID(EmployeeId).ID;       
            filters.Add("EmployeeID", employeeId);

            GridViewAssignments1.SetGridView(filters, true);

            GridViewAssignments1.DisableGridViewCells(State);
        }

        private void GotoAssignmentDetailes(int assignmentId)
        {
            APageData.Set(new AssignmentDetailsPageData(assignmentId, GUIServices.PAGE_UPDATE_EMPLOYEE));

            Response.Redirect(GUIServices.PAGE_UPDATE_ASSIGNMENT);
        }

        private void GetPageData()
        {
            EmployeeDetailsPageData data = APageData.Get<EmployeeDetailsPageData>();
            //Session.Remove(GUIServices.DATA_DETAILS_EMPLOYEE);
            EmployeeId = data.EmployeeId;
            SourcePage = data.SourcePage;
        }

        private void SetButtons()
        {
            ButtonUpdateEmployee.Text = BUTTON_ENABLE_UPDATE;
            DbConstants.Roles role = State.UserRole;
            if (role == DbConstants.Roles.HeadDepartment) ButtonUpdateEmployee.Visible = false;
        }

        protected void LinkButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(SourcePage);
        }

        protected void ButtonUpdateEmployee_Click(object sender, EventArgs e)
        {
            if (EmployeeDetails1.IsReadonly) //enable details update
            {
                EmployeeDetails1.ReadonlyControls(false);
                ButtonUpdateEmployee.Text = BUTTON_UPDATE_EMPLOYEE;
                PanelUpdateMessage.Visible = false;
            }
            else if (EmployeeDetails1.UpdateEmployee()) //update was successful
            {
                EmployeeDetails1.ReadonlyControls(true);
                ButtonUpdateEmployee.Text = BUTTON_ENABLE_UPDATE;
                PanelUpdateMessage.Visible = true;
            }
        }
    }

    public class EmployeeDetailsPageData : APageData
    {
        public string EmployeeId { get; private set; }
        public string SourcePage { get; private set; }

        public EmployeeDetailsPageData(string employeeId, string sourcePage)
        {
            EmployeeId = employeeId;
            SourcePage = sourcePage;
        }
    }
}
